home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1997
/
MacHack 1997.toast
/
Hacks
/
Hacks ’96
/
FinderFlocks
/
FinderFun
/
AppleEventUtilities.h
< prev
next >
Wrap
Text File
|
1996-06-22
|
12KB
|
348 lines
/*
File: AppleEventUtilities.h
Contains: C++ Wrappers for Apple Event manager
*/
#ifndef __APPLEEVENTUTILITIES__
#define __APPLEEVENTUTILITIES__
//
// Needed for AEDesc & c.
//
#ifndef __APPLEEVENTS__
#include "AppleEvents.h"
#endif
const DescType typeTokenObject = 'tkob';
const DescType cAbstractObject = 'abst';
const DescType cAbstractWindow = 'abwn';
// These should be generic types
const typeDateTimeRec = 'dtim';
// const typeLongDateTime = 'ldt ';
const typeLongDateTimeRec = 'ldtr';
// Use with CreateList:
#define kMakeAEList false
#define kMakeAERecord true
// Some procedural routines
Boolean InterpretCompareResult(DescType comparisonOperator, long key1, long key2 );
Boolean GeneralCompare(DescType comparisonOperator, Ptr data, Size length, Ptr compareWith, Size comparisonLength, Boolean caseSensitive);
class TTokenDescriptor;
class TDescriptor
{
protected:
//
// A TDescriptor is an AEDesc, so it must have
// two and only two fields, and they must be
// the descriptor type and the data handle.
//
DescType fDescriptorType;
Handle fDataHandle;
//
// These flags are passed to AEResolve when
// specific flags are not specified. This
// allows an application to define kAEIDoWhose
// and/or kAEIDoMarking once at startup, and
// subsequentially call AEResolve without
// any parameters elsewhere in the code.
//
static short fCallbackFlags;
public:
static void SetCallbackFlags(short callbackFlags) { fCallbackFlags = callbackFlags; };
//
// Construction methods
//
TDescriptor() : fDescriptorType(typeNull), fDataHandle(nil) {};
TDescriptor(DescType type, Handle dataHandle) : fDescriptorType(type), fDataHandle(dataHandle) {};
// TDescriptor(const TDescriptor desc) : fDescriptorType(desc.DescriptorType()), fDataHandle(desc.DataHandle()) {};
//
// Dispose
//
void Dispose();
//
// Conversion operators
//
operator const AEDesc*() const { return (const AEDesc*) this; };
operator AEDesc*() { return (AEDesc*) this; };
operator AEDesc() { return *((AEDesc*) this); };
//
// Direct access to descriptor information
//
DescType DescriptorType() const { return fDescriptorType; };
Handle DataHandle() { return fDataHandle; };
Boolean IsNullDescriptor() const { return fDescriptorType == typeNull; };
Boolean DataHandleIsNil() const { return fDataHandle == nil; };
//
// Content manipulation
//
OSErr AttemptCoercion(DescType typeToCoerceTo);
void CoerceInPlace(DescType typeToCoerceTo);
TDescriptor Coerce(DescType typeToCoerceTo) const;
void CoerceToStandardType();
TDescriptor Clone() const;
void CopyDesc(const TDescriptor& desc);
void AdoptDesc(TDescriptor& desc);
void AdoptHandle(DescType dataType, Handle dataHandle);
void CopyData(const DescType typeCode, const Ptr data, const Size length);
void CreateList(Boolean isRecord = kMakeAEList, Ptr factoringPtr = nil, Size factoredSize = 0);
void MakeNull();
//
// Descriptor creation
//
void MakeBoolean(const Boolean data);
void MakeLong(const long data);
void MakePoint(const Point& thePoint);
void MakePoint(const short h, const short v);
void MakeRect(const Rect& theRect);
void MakeUnsignedLong(const unsigned long data);
void MakeEnumeration(const DescType enumeration);
void MakeDescType(const DescType data);
void MakeKeyword(const AEKeyword theKeyword);
void MakeOrdinal(const DescType data);
void MakeTypeOrInteger(const DescType data);
void MakeString(Str255 data);
void MakeDateTimeRec(const DateTimeRec dateTime);
void MakeDateTimeRec(const long dateTime);
void MakeLongDateTimeRec(const LongDateRec dateTime);
void MakeLongDateTimeRec(const DateTimeRec dateTime);
void MakeLongDateTimeRec(LongDateTime lsecs);
void MakeLongDateTime(const LongDateTime lsecs);
void MakeFSS(const FSSpec& spec);
void AdoptAlias(Handle alias);
void MakeAlias(FSSpec& spec);
void MakeProcessSerialNumber(ProcessSerialNumber psn);
void MakeObjectSpecifier(DescType desiredClass, TDescriptor container, DescType keyForm, TDescriptor keyData, Boolean disposeInputs);
void MakeCompDescriptor(DescType comparisonOperator, DescType propertyIdentifier, TDescriptor compareWith, Boolean disposeInputs);
//
// Data extraction
//
void GetBlock(Ptr data, Size length, DescType desiredType) const;
long GetLong() const;
Boolean GetBoolean() const;
DescType GetDescType() const;
AEKeyword GetKeyword() const;
DescType GetEnumeration() const;
DescType GetOrdinal() const;
Point GetPoint() const;
Rect GetRect() const;
void GetString(Str255) const;
DateTimeRec GetDateTimeRec() const;
void GetFSS(FSSpec& spec) const;
Boolean Compare(DescType comparisonOperator, Str255 withString) const;
Boolean Compare(DescType comparisonOperator, TDescriptor& compareWith) const;
//
// List methods
//
// Formerly, we had a separate class, TDescriptorList.
// Now, however, there is no difference between a TDescriptor
// and a TDescriptorList
//
void MakeEmptyList();
void MakeList();
long CountItems() const;
TDescriptor GetNthDescriptor(long index, DescType desiredType = typeWildCard, AEKeyword* key = nil) const;
void AddDescriptor(long index, TDescriptor& data);
void AddDescriptor(TDescriptor& data);
void AddData(long index, DescType descType, Ptr data, Size length);
void AddData(DescType descType, Ptr data, Size length);
void AddLong(long number);
void AddType(DescType descType);
void AppendList(const TDescriptor& list);
void AdoptList(TDescriptor* list);
//
// AERecord methods
//
// Formerly, we had a separate class, TAERecord.
// Now, however, AERecords and AEDescriptors are
// used interchangably enough that there is really
// no reason to have a separate class.
//
void MakeAERecord();
TDescriptor GetDescriptor(AEKeyword key, DescType desiredType = typeWildCard);
TDescriptor GetOptionalParameter(AEKeyword key, DescType desiredType = typeWildCard);
void GetParameterPtr(AEKeyword key, DescType desiredType, DescType *typeCode, Ptr dataPtr, Size maximumSize, Size *actualSize);
long GetLongParameter(AEKeyword key, DescType desiredType = typeLongInteger);
short GetShortParameter(AEKeyword key, DescType desiredType = typeShortInteger);
TDescriptor GetDirectObject();
long GetErrorCode();
void PutDescriptor(AEKeyword key, TDescriptor data);
void PutParameterPtr(AEKeyword key, DescType typeCode, Ptr dataPtr, Size dataSize);
void PutParameterHandle(AEKeyword key, DescType typeCode, Handle dataHandle);
void PutLongParameter(AEKeyword key, DescType typeCode, long data);
void PutLongParameter(AEKeyword key, long data);
void PutShortParameter(AEKeyword key, DescType typeCode, short data);
void PutShortParameter(AEKeyword key, short data);
void PutResult(TDescriptor data);
void PutErrorCode(long theErr);
//
// Object support library methods
//
TTokenDescriptor Resolve();
TTokenDescriptor Resolve(short callbackFlags);
};
//
// A descriptor loop iterates over every item in a descriptor
// list or an AERecord.
//
class TDescriptorLoop
{
public:
TDescriptorLoop(const TDescriptor* dl) {fDescriptorList = dl; fIndex = 0; fCount = 0; };
Boolean Next(TDescriptor& d, AEKeyword* key = nil, Handle* h = nil);
private:
const TDescriptor* fDescriptorList;
long fIndex;
long fCount;
};
#define FOREACHDESCRIPTOR(listToIterate, descriptor) \
TDescriptorLoop loop(listToIterate); \
AEKeyword keyword; \
while (loop.Next(descriptor, &keyword, nil))
class TAEvent : public TDescriptor
{
public:
TAEvent();
//
// Conversion opperators
//
operator const AppleEvent*() const { return (const AppleEvent*) this; };
operator AppleEvent*() { return (AppleEvent*) this; };
operator AppleEvent() { return *((AppleEvent*) this); };
void MakeAppleEvent(AEEventClass eventClass, AEEventID eventID,
const TDescriptor& target, short returnID = kAutoGenerateReturnID, long transactionID = kAnyTransactionID);
void MakeAppleEvent(AEEventClass eventClass, AEEventID eventID,
const ProcessSerialNumber& psn, short returnID = kAutoGenerateReturnID, long transactionID = kAnyTransactionID);
void MakeEventAddressedToSelf(AEEventClass eventClass, AEEventID eventID, short returnID = kAutoGenerateReturnID, long transactionID = kAnyTransactionID);
void MakeEventAddressedToSystem(AEEventClass eventClass, AEEventID eventID, short returnID = kAutoGenerateReturnID, long transactionID = kAnyTransactionID);
void Send(TAEvent* reply, AESendMode sendMode, AESendPriority sendPriority = kAENormalPriority,
long timeOutInTicks = kAEDefaultTimeout, AEIdleUPP idleProc = nil, AEFilterUPP filterProc = nil);
void Send(TAEvent* future) { this->Send(future, kAEWaitReply, kAENormalPriority, 0); };
void SendNoReply() { TAEvent reply; this->Send(&reply, kAENoReply); }
void SendNoExecute();
// misc
void SuspendTheCurrentEvent();
void ResumeTheCurrentEvent(TAEvent* reply, AEEventHandlerUPP dispatcher = nil, long refCon = 0);
void SetTheCurrentEvent();
void ResetTimer();
void SpecifyThatParameterIsOptional(AEKeyword theOptionalKeyword);
// get and set attributes for the Apple event
TDescriptor GetAttribute(AEKeyword key, DescType desiredType = typeWildCard);
long GetLongAttribute(AEKeyword key);
long GetReturnID() { return this->GetLongAttribute(keyReturnIDAttr); };
long GetMessageTimeout() { return this->GetLongAttribute(keyTimeoutAttr); };
void PutAttribute(AEKeyword key, TDescriptor attribute);
void PutOptionalDescriptor(AEKeyword key, TDescriptor data);
void PutLongAttribute(AEKeyword key, long attributeValue);
/*
OSErr GetAttribute(AEKeyword key, DescType desiredType, TDescriptor* descriptor);
OSErr GetAttributePtr(AEKeyword key, DescType desiredType, DescType *typeCode,
Ptr dataPtr, Size maximumSize, Size *actualSize);
OSErr SizeOfAttribute(AEKeyword key, DescType desiredType, Size *size);
OSErr PutAttributePtr(AEKeyword key, DescType desiredType, Ptr dataPtr, Size dataSize);
*/
};
//
// AppleEventUtilities doesn't know anything about TAbstractScriptableObject
// other than the fact that a class with that name exists.
//
class TAbstractScriptableObject;
typedef TTokenDescriptor (*MakeTokenDescriptorProcPtr)();
typedef TTokenDescriptor (*ProcessDescriptorProcPtr)(TDescriptor);
typedef TAbstractScriptableObject* (*MergeTokensProcPtr)(TAbstractScriptableObject* baseToken, TAbstractScriptableObject* mergeToken);
void InstallNullContainerCreationProc(MakeTokenDescriptorProcPtr creationProc);
void InstallPreResolveProc(ProcessDescriptorProcPtr preResolveProc);
void InstallMergeTokensProc(MergeTokensProcPtr mergeTokensProc);
TTokenDescriptor CreateNullContainerToken();
class TTokenDescriptor : public TDescriptor
{
public:
TTokenDescriptor();
TTokenDescriptor(TDescriptor desc);
// destructor & disposer
void DisposeToken();
TAbstractScriptableObject* TokenHandle();
void AdoptToken(TTokenDescriptor& tokenDescriptor);
void AdoptToken(TAbstractScriptableObject* tokenObject);
};
#define FOREACHTOKEN(tokenList, tokenDescriptor) \
TDescriptorLoop loop(tokenList); \
TAbstractScriptableObject* token; \
while (loop.Next(tokenDescriptor, nil, nil) && ((token = tokenDescriptor.TokenHandle()) != nil))
#endif